package com.wxzhou.wangzhuan.api;

import com.wxzhou.wangzhuan.dto.*;
import com.wxzhou.wangzhuan.exception.UserAccountException;
import com.wxzhou.wangzhuan.security.UserContext;
import com.wxzhou.wangzhuan.service.UserAccountService;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;

import java.util.Map;

/**
 * 用户账户
 */
@Path("/api")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UserAccountApi {

    private static final Logger LOG = Logger.getLogger(UserAccountApi.class);

    @Inject
    UserContext userContext;

    @Inject
    UserAccountService userAccountService;

    /**
     * 查询用户收入明细
     *
     * @param userIncomeRecordRequestDto
     * @return
     */
    @POST
    @Path("/account-record")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<Response> findBindRecord(@Valid UserIncomeRecordRequestDto userIncomeRecordRequestDto) {
        boolean authenticated = userContext.isAuthenticated();
        if (!authenticated) {
            ResponseDto<Object> unauthorized = ResponseDto.unauthorized("");
            return Uni.createFrom().item(Response.status(unauthorized.getCode()).entity(unauthorized).build());
        }
        return userAccountService.getPageList(userIncomeRecordRequestDto, userContext.getCurrentUserInfo().get())
                .onItem().transform(response -> {
                    if (response != null) {
                        ResponseDto<PageResponseDto<UserIncomeRecordResponseDto>> success = ResponseDto.success(response);
                        return Response.ok(success).build();
                    } else {
                        ResponseDto<Map<String, Object>> resp = ResponseDto.fail("查询失败");
                        return Response.status(500).entity(resp).build();
                    }
                })
                .onFailure().recoverWithUni(throwable -> {
                    String msg = "";
                    if (throwable instanceof UserAccountException) {
                        msg = throwable.getMessage();
                    }
                    ResponseDto<Map<String, Object>> resp = ResponseDto.error("查询收入明细失败:" + msg);
                    return Uni.createFrom().item(Response.status(500).entity(resp).build());
                });
    }

    /**
     * 获取用户账户面板数据
     *
     * @return
     */
    @POST
    @Path("/account-dashboard")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<Response> getAccountDashboardByUser(@Valid CommonRequestDto commonRequestDto) {
        boolean authenticated = userContext.isAuthenticated();
        if (!authenticated) {
            ResponseDto<Object> unauthorized = ResponseDto.unauthorized("");
            return Uni.createFrom().item(Response.status(unauthorized.getCode()).entity(unauthorized).build());
        }
        return userAccountService.getAccountDashboardByUser(userContext.getCurrentUserInfo().get())
                .onItem().transform(response -> {
                    if (response != null) {
                        ResponseDto success = ResponseDto.success(response);
                        return Response.ok(success).build();
                    } else {
                        ResponseDto success = ResponseDto.success(new UserAccountDashboardResponseDto());
                        return Response.ok(success).build();
                    }
                })
                .onFailure().recoverWithUni(throwable -> {
                    throwable.printStackTrace();
                    String msg = "";
                    if (throwable instanceof UserAccountException) {
                        msg = throwable.getMessage();
                    }
                    ResponseDto<Map<String, Object>> resp = ResponseDto.error("查询失败用户账户面板数据异常:" + msg);
                    return Uni.createFrom().item(Response.status(500).entity(resp).build());
                });
    }

    /**
     * 用户收益面板+明细,分页查询
     *
     * @param commonRequestDto
     * @return
     */
    @POST
    @Path("/income-dashboard")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<Response> getIncomeAccountDashboardByUser(@Valid UserIncomeDashboardRequestDto commonRequestDto) {
        boolean authenticated = userContext.isAuthenticated();
        if (!authenticated) {
            ResponseDto<Object> unauthorized = ResponseDto.unauthorized("");
            return Uni.createFrom().item(Response.status(unauthorized.getCode()).entity(unauthorized).build());
        }
        return userAccountService.getAccountDashByDayRange(commonRequestDto,userContext.getCurrentUserInfo().get())
                .onItem().transform(response -> {
                    if (response != null) {
                        ResponseDto success = ResponseDto.success(response);
                        return Response.ok(success).build();
                    } else {
                        ResponseDto success = ResponseDto.success(new UserAccountDashboardResponseDto());
                        return Response.ok(success).build();
                    }
                })
                .onFailure().recoverWithUni(throwable -> {
                    String msg = "";
                    if (throwable instanceof UserAccountException) {
                        msg = throwable.getMessage();
                    }
                    ResponseDto<Map<String, Object>> resp = ResponseDto.error("查询失败" + msg);
                    return Uni.createFrom().item(Response.status(500).entity(resp).build());
                });
    }

}
